package com.basic.modules.nykf.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.R;
import com.basic.modules.nykf.dao.CustomerComplaintHandlingDao;
import com.basic.modules.nykf.entity.CustomerComplaintHandlingEntity;
import com.basic.modules.nykf.service.CustomerComplaintHandlingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;


@Service
public class CustomerComplaintHandlingServiceImpl extends ServiceImpl<CustomerComplaintHandlingDao, CustomerComplaintHandlingEntity>
        implements CustomerComplaintHandlingService {

    @Autowired
    private CustomerComplaintHandlingDao customerComplaintHandlingDao;

    /**
     * 新增投诉明细
     * @param customerComplaintHandlingEntity
     * @return
     */
    @Override
    public R saved(CustomerComplaintHandlingEntity customerComplaintHandlingEntity) {
        customerComplaintHandlingEntity.setJqsj(new Date());
        if ("已完结".equals(customerComplaintHandlingEntity.getWjztVal())) {
            customerComplaintHandlingEntity.setWjsj(new Date());
        }
        if (StrUtil.isNotEmpty(customerComplaintHandlingEntity.getTsjgpdVal())){
            customerComplaintHandlingEntity.setPdsj(new Date());
        }
        customerComplaintHandlingDao.insert(customerComplaintHandlingEntity);
        Long complaintDetailId = customerComplaintHandlingEntity.getComplaintDetailId();
        return R.ok(complaintDetailId);
    }
    /**
     * 分页查询
     * @param params
     * @return
     */
    @Override
    public PageUtils getCustomerComplaintSuggestionDetailList(ParamsModel<CustomerComplaintHandlingEntity> params) {
        int pageNum = params.getPage().getIndex().intValue();
        int pageSize = params.getPage().getSize().intValue();
        int offset = (pageNum - 1) * pageSize;
        List<Map> customizeQueryCondition = params.getCondition().getCustomizeQueryCondition();
        String keyword = params.getCondition().getKeyword();
        Long complaintId = params.getCondition().getComplaintId();

        // 获取当前页的客诉登记表主键列表
        List<Integer> ksdjIds = customerComplaintHandlingDao.getKsdjIds(offset, pageSize, customizeQueryCondition, keyword, complaintId);

        if (ksdjIds.isEmpty()) {
            return new PageUtils(new ArrayList<>(), 0, pageSize, pageNum);
        }

        // 获取客诉登记和客诉明细的完整数据
        List<CustomerComplaintHandlingEntity> rawList = customerComplaintHandlingDao.getKsmx(ksdjIds, customizeQueryCondition, keyword, complaintId);

        int totalCount = customerComplaintHandlingDao.getTotal(customizeQueryCondition, keyword, complaintId);
        return new PageUtils(rawList, totalCount, pageSize, pageNum);
    }

    /**
     * 面板分析
     * @param channel
     * @param clr
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @Override
    public Map<String, Object> getFxRate(String channel, String clr, String startTime, String endTime) throws ParseException {

        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = inputFormat.parse(startTime);
        Date endDate = inputFormat.parse(endTime);
        String str1 = "有效";
        String str2 = "无效";

        //登记的总量
        Integer djTotail = customerComplaintHandlingDao.getTotail(channel, startDate, endDate, clr);
        if (djTotail == null || djTotail == 0) {
            return Collections.emptyMap();
        }

        //有效数量
        Integer yxCount = customerComplaintHandlingDao.getYxsl(channel, str1, startDate, endDate, clr);
        //无效数量
        Integer wxCount = customerComplaintHandlingDao.getWxsl(channel, str2, startDate, endDate, clr);
        //未处理的数量
        Integer wclCount = djTotail - yxCount - wxCount;

        // 计算有效率（保留两位小数）
        double yxRate = djTotail == 0 ? 0.0 : ((double) yxCount / djTotail * 100);
        String yxRateStr = String.format("%.2f", yxRate);

        //登记的准确量
        Integer zqCount = customerComplaintHandlingDao.getDjzqsl(channel, startDate, endDate, clr);

        //登记的错误量
        Integer cwCount = djTotail - zqCount - wclCount;

        //登记的准确率
        double zqRate = djTotail == 0 ? 0.0 : ((double) zqCount / djTotail * 100);
        String zqRateStr = String.format("%.2f", zqRate);

        //计算当前时间
        LocalDateTime nowDate = LocalDateTime.now();
        //12小时的接起率
        Integer jqsl = customerComplaintHandlingDao.getJqsl(channel, startDate, endDate, nowDate, clr);
        double jqRate = 0.0;
        if (djTotail > 0) {
            jqRate = ((double) jqsl / djTotail) * 100;
        }
        String jqRateStr = String.format("%.2f", jqRate);

        //24小时的判定率
        Integer pdsl = customerComplaintHandlingDao.getPdsl(channel, startDate, endDate, nowDate, clr);
        double pdRate = 0.0;
        if (djTotail > 0) {
            pdRate = ((double) pdsl / djTotail) * 100;
        }
        String pdRateStr = String.format("%.2f", pdRate);

        //48小时的完结率
        Integer wjsl = customerComplaintHandlingDao.getWjsl(channel, startDate, endDate, nowDate, clr);
        double wjRate = 0.0;
        if (djTotail > 0) {
            wjRate = ((double) wjsl / djTotail) * 100;
        }
        String wjRateStr = String.format("%.2f", wjRate);

        Map<String, Object> map = new HashMap();
        map.put("zqCount", zqCount);
        map.put("cwCount", cwCount);
        map.put("yxCount", yxCount);
        map.put("wxCount", wxCount);
        map.put("wclCount", wclCount);
        map.put("yxRateStr", yxRateStr);
        map.put("zqRateStr", zqRateStr);
        map.put("jqRateStr", jqRateStr);
        map.put("pdRateStr", pdRateStr);
        map.put("wjRateStr", wjRateStr);
        return map;
    }

    @Override
    public Map<String, Object> getByDate(String djsj) {
        Map map =  new HashMap();
        Integer count = customerComplaintHandlingDao.getByDate(djsj);
        map.put("count",count);
        //如果count大于0,说明有明细
        if (count > 0) {
            Date minJqsj = customerComplaintHandlingDao.getMinJqsj(djsj);
            map.put("minJqsj",minJqsj);
        }
        return map;
    }




}


